package com.yunquan.ohana.ui.calendar.profiles.add

import androidx.databinding.ObservableField
import com.yunquan.ohana.base.binding.command.BindingAction
import com.yunquan.ohana.base.binding.command.BindingCommand
import com.yunquan.ohana.base.ui.BaseViewModel
import com.yunquan.ohana.base.ui.ItemViewModel
import com.yunquan.ohana.ui.calendar.lists.add.ListsAddViewModel

class ColorItemViewModel<T : BaseViewModel>(viewModel: T) : ItemViewModel<T>(viewModel) {

    val color = ObservableField<Int>()
    val checked = ObservableField(false)

    constructor(viewModel: T, color: Int, checked: Boolean = false) : this(viewModel) {
        this.color.set(color)
        this.checked.set(checked)
    }

    val onSelectClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            if (checked.get() != true) {
                if (viewModel is AddProfilesViewModel) {
                    viewModel.items.forEach {
                        if (it != this@ColorItemViewModel) {
                            it.checked.set(false)
                        }
                    }
                    viewModel.selectedColor.set(color.get()!!)
                } else if (viewModel is ListsAddViewModel) {
                    viewModel.items.forEach {
                        if (it != this@ColorItemViewModel) {
                            it.checked.set(false)
                        }
                    }
                    viewModel.selectedColor.set(color.get()!!)
                }
                checked.set(true)
            }
        }
    })
}